﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

namespace ueEngine.Framework.Empires
{
    public abstract class BaseCommander
    {
        private static BaseAnalyzer analyzer = null;
        public BaseCommander()
        {
            string type = Configuration.ConfigReader.Empires.Analyzer.Value;
            string assembly = Configuration.ConfigReader.Empires.Analyzer.Assembly;
            if (String.IsNullOrEmpty(type))
            {
                throw new NullReferenceException("配置参数有误，请加成empires节点配置");
            }
            var instance = Activator.CreateInstanceFrom(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "bin\\" + assembly,
                type);
        }
        protected abstract string GetTempleteBasePath(string identity);
        protected abstract string GetPageTitleAndTitle(string identity);

        public HTMLPack Analysis(string uri, string identity)
        {
            string basePath = GetTempleteBasePath(identity);
            if (!(basePath.EndsWith("\\") || basePath.EndsWith("/")))
            {
                basePath += "\\";
            }
            var appBasePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //应用程序根目录
            if (!(appBasePath.EndsWith("\\") || appBasePath.EndsWith("/")))
            {
                appBasePath += "\\";
            }
            string customerTempletePath = String.Format("{0}{1}{2}", appBasePath, basePath, uri); //获得模板文件的物理地址
            string textStream = FileLoader.LoadFile(customerTempletePath); //获得文件流
            string head = string.Empty;
            string body = string.Empty;
            return new HTMLPack()
            {
                Head = analyzer.HeadAnalyzer(identity, textStream, customerTempletePath).Replace("*r*n", "\r\n"),
                Body = analyzer.BodyAnalyzer(identity, textStream, customerTempletePath).Replace("*r*n", "\r\n")
            };
            //Regex reg = new Regex(PageBuilderRegexpLib.HeadPicker);
            //if (reg.IsMatch(textStream))//匹配head字段
            //{
            //    head = HeadAnalyzer.Analysis(reg.Match(textStream).Groups[0].ToString(), customerTempletePath,
            //        identity).Replace("*r*n", "\r\n");
            //}
            //reg = new Regex(PageBuilderRegexpLib.BodyPicker);
            //if (reg.IsMatch(textStream)) //匹配body字段
            //{
            //    body = BodyAnalyzer.Analysis(reg.Match(textStream).Groups[0].ToString(), customerTempletePath,
            //        identity).Replace("*r*n", "\r\n");
            //}
            //return new HTMLPack() { Body = body, Head = head };
        }
    }
}
